<!DOCTYPE html>
<html>
<head>
    <title>video form</title>
    <#include "/header.html">
</head>
<body class="bg-white">
<div id="appMain">
    <form class="layui-form layuimini-form" action="" lay-filter="dataForm">
        <div class="layui-form-item">
            <label class="layui-form-label">封面类目名称</label>
            <div class="layui-input-block">
                <input type="text" name="title" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">封面副标题</label>
            <div class="layui-input-block">
                <input type="text" name="subtitle" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">图片</label>
            <div class="layui-form-mid">
                <input type="hidden" name="image" value="">
                <div class="layui-upload">
                    <button type="button" class="layui-btn layui-btn-sm layui-bg-black" id="uploadBtn">选择图片</button>
                    <div class="layui-upload-list">
                        <img class="layui-upload-img" id="uploadPreview" src="">
                        <p id="uploadText"></p>
                    </div>
                </div>
            </div>
        </div>
      <div class="layui-form-item">
            <label class="layui-form-label">资源类型</label>
            <div class="layui-input-block">
                <select id="categoryId" name="categoryId" lay-search>
      </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">订阅付费金额</label>
            <div class="layui-input-block">
                <input type="text" name="amount" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">付费类型</label>
            <div class="layui-input-block">
                <input type="radio" name="payType" value="0" title="免费">
                <input type="radio" name="payType" value="1" title="付费">
            </div>
        </div>
        <div class="layui-form-item layui-hide">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" id="commitBtn" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
        </div>
    </form>
</div>
<script type="text/javascript">
    layui.use(['form', 'laydate', 'upload'], function () {
        var form = layui.form,
        upload = layui.upload,
            $ = layui.$;
        var laydate = layui.laydate;
        var keywords = [];

        var _parent = parent;
        var image="";
      //获取类型select
  	  $.ajax({
  			url:serverUrl('video/package/category/studey'),
  			type:"post",
  			dataType:"",
  			success:function(btn){
  		          var select = $("#categoryId");
  			      	for (var j = 0; j < btn.count; j++) {
  				      	  select.append("<option value='"+btn.data[j].id+"'>"+btn.data[j].name+"</option>");
  			  		}
  		          layui.form.render("select");
  			},
  			error:function(){
  				console.log("类型获取失败");
  			}
  		})
        $(document).ready(function () {
            if (parent.formDataId > 0) {
                $.ajax({
                    type: "POST",
                    url: serverUrl('video/package/info/' + parent.formDataId),
                    data: {},
                    dataType: "json",
                    success: function (result) {
                        if (result.code === 0) {
                            form.val('dataForm', result.info);
	                        if (result.info.image != '') {
	                            $('#uploadPreview').attr('src', result.info.image);
	                        }
                        }
                    }
                });
            }
        });
        var submitForm = function () {
            var params = form.val('dataForm');
            params['id'] = _parent.formDataId || null;

            var method = _parent.formDataId > 0 ? 'update' : 'save';
            submitFormData('video/package/'+method, params);
        };

        //普通图片上传
        var uploadInst = upload.render({
            elem: '#uploadBtn'
            ,url: serverUrl('oss/file/upload')
            ,auto: false
            ,choose: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#uploadPreview').attr('src', result); //base64
                });
            }
            ,done: function(res){
                if(res.code > 0){
                    return layer.msg('上传失败');
                }
                form.val('dataForm', {'image': res.url});
                submitForm();
            }
        });

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var img = $('#uploadPreview').attr('src');
            if (!img) {
                layer.msg('请选择图片', {icon: 2, anim: 6});
                return false;
            }
            if (img.indexOf('data:') === 0) {
                uploadInst.upload();
                return false;
            }
            submitForm();
            return false;
        });

    });
</script>
</body>
</html>